//
//  MyViewController.m
//  Qyer
//
//  Created by 赵宇 on 15/9/8.
//  Copyright (c) 2015年 赵宇. All rights reserved.
//

#import "MyViewController.h"
#import "BaseHeader.h"
@interface MyViewController ()<UITableViewDelegate, UITableViewDataSource,UIScrollViewDelegate,UIGestureRecognizerDelegate>
@end

@implementation MyViewController

-(void)dealloc
{
    [_nav release];
    [_map release];
    [_table release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = YES;
    self.num = [[DataBase shareDataBase]selectALL].count;
    if (!_nav) {
        
        [self creatNav];
    }
    [_table reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:240./255 green:240./255 blue:240./255 alpha:1];
    
    // 签完UIGestureRecognizerDelegate代理,然后写这两句话,效果就是从这个页面push的其他页面都可以右滑返回上一页
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;

    [self buildView];
}
-(void)creatNav
{
    self.nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.nav.backgroundColor = [UIColor colorWithRed:34./255 green:169./ 255 blue:118./255 alpha:1];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 30)];
    title.text = @"设 置";
    title.textColor = [UIColor whiteColor];
    title.font = [UIFont systemFontOfSize:17.];
    title.textAlignment = YES;
    [self.nav addSubview:title];
    // 夜间模式
    UIButton *night = [UIButton buttonWithType:UIButtonTypeCustom];
    night.frame = CGRectMake(SCREEN_WIDTH-40, 25, 30, 30);
    [night setImage:[UIImage imageNamed:@"night"] forState:UIControlStateNormal];
    [night setImage:[UIImage imageNamed:@"sun"] forState:UIControlStateSelected];
    [night addTarget:self action:@selector(changeMode:) forControlEvents:UIControlEventTouchUpInside];
    night.titleLabel.font = [UIFont systemFontOfSize:14.];
    [self.nav addSubview:night];
    [self.view addSubview:self.nav];
    
    [_nav release];
    [title release];
    
}
-(void)buildView
{
    self.map = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/5*2)];
    [self.map setImage:[UIImage imageNamed:@"profileHeader.jpg"]];
    
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-48) style:UITableViewStylePlain];
    self.table.tableHeaderView = self.map;
    [self.table setDelegate:self];
    [self.table setDataSource:self];
    UIView *whiteView = [[UIView alloc]init];
    self.table.tableFooterView = whiteView;
    [self.view addSubview:self.table];
    [_map release];

}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.table) {
        if (scrollView.contentOffset.y < 0) {
       // 下拉放大,CGAffineTransformMakeScale求放大比例,然后再手动把table.center拉上去
        self.table.transform = CGAffineTransformMakeScale((self.table.frame.size.height - scrollView.contentOffset.y)/self.table.frame.size.height, (self.table.frame.size.height - scrollView.contentOffset.y)/self.table.frame.size.height);
        self.table.center = CGPointMake(SCREEN_WIDTH/2, self.table.frame.size.height/2+scrollView.contentOffset.y);
        }
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT/12;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myViewTableCellid = @"myViewTableCellID";
    MyViewTableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:myViewTableCellid];
    if (!mycell) {
        mycell = [[MyViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myViewTableCellid];
    }
    
    if (indexPath.row == 0) {
        [mycell.countLab setText:[NSString stringWithFormat:@"❤️收藏目的地个数 : %ld",self.num]];
    }

    NSArray *picArr = @[@"start",@"clear",@"about"];
    NSArray *titleArr = @[@"收藏",@"清除缓存",@"关于"];
    [mycell setupPic:[picArr objectAtIndex:indexPath.row] titleText:[titleArr objectAtIndex:indexPath.row]];
    return mycell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CollectViewController *collect = [[CollectViewController alloc]init];
        [self.navigationController pushViewController:collect animated:YES];
    }
    if (indexPath.row == 1) {
        NSUInteger size = [[SDImageCache sharedImageCache]getSize];
        CGFloat cache = size/1024./1024;
        NSString *str = [NSString stringWithFormat:@"一共清除了%.2fM",cache];
        [[SDImageCache sharedImageCache]clearDisk];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    if (indexPath.row == 2) {
        AboutViewController *aboutV = [[AboutViewController alloc]init];
        [self.navigationController pushViewController:aboutV animated:YES];
    }
    
}
-(void)changeMode:(UIButton *)sender
{
    if (sender.selected == NO) {
        sender.selected = YES;
        // 消息中心,发送消息
        [[NSNotificationCenter defaultCenter]postNotificationName:@"Night" object:nil ];
        // 根据observer和name移除
        [[NSNotificationCenter defaultCenter]removeObserver:self name:@"Night" object:nil];
       
    }else{
        sender.selected = NO;
        // 消息中心,发送消息
        [[NSNotificationCenter defaultCenter]postNotificationName:@"Day" object:nil ];
        // 根据observer和name移除
         [[NSNotificationCenter defaultCenter]removeObserver:self name:@"Day" object:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
